﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WpfPageTransitions;
using GestionDeCinebox.UC;
using GestionDeCinebox.Classes;
using System.Data.SqlClient;
using System.Data;

namespace GestionDeCinebox
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public static Personne personne = null;
        public static List<UserControl> pages = new List<UserControl>();
        int soTrang = 0;
        private DataProvider provider;

        public DataProvider Provider
        {
            get { return provider; }
            set { provider = value; }
        }

        public MainWindow()
        {
            InitializeComponent();
            this.WindowState = WindowState.Maximized;
            provider = new DataProvider();
            provider.Connect();

            pages.Add(new ucHome(this));
            pages.Add(new ucCalendrier(this));
            pages.Add(new ucFilm(this));
            pages.Add(new ucIntroduction(this));

            pageTransitionControl.ShowPage(pages[soTrang]);

            btnGestion.Visibility = Visibility.Hidden;
        }

        public void ShowPage(UserControl uc)
        {
            pageTransitionControl.ShowPage(uc);
        }

        private void SetTransition(int sotrang)
        {
            if (sotrang > 0 && sotrang <= 2)
            {
                pageTransitionControl.TransitionType = (PageTransitionType)Enum.Parse(typeof(PageTransitionType), "GrowAndFade", true);
            }
            else if (sotrang == 3)
            {
                pageTransitionControl.TransitionType = (PageTransitionType)Enum.Parse(typeof(PageTransitionType), "FlipAndFade", true);
            }
            else
            {
                pageTransitionControl.TransitionType = (PageTransitionType)Enum.Parse(typeof(PageTransitionType), "SpinAndFade", true);
            }
        }

        private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            pageTransitionControl.Width = this.ActualWidth;
            for (int i = 0; i < pages.Count; i++)
            {
                pages[i].Width = this.ActualWidth;
            }
        }

        private void btnEntrer_Click(object sender, RoutedEventArgs e)
        {
            String s = btnEntrer.Content.ToString();
            if("Entrer".Equals(s))
            {
                childEntrer.Show();
            }
            else
            {
                //log out
                personne = null;
                btnGestion.Visibility = Visibility.Hidden;
                btnEntrer.Content = "Entrer";
                txtNomUser.Text = "SGBD Projet [0912104 - 0912186]";
            }
        }

        private void btnSignUp_Click(object sender, RoutedEventArgs e)
        {
            DateTime d = new DateTime(1991, 1, 1);

            dtNaissance.Value = d;
            childEnregis.Show();
        }

        private void btnEnregistrer_Click(object sender, RoutedEventArgs e)
        {
            using(SqlCommand cmd = new SqlCommand("dbo.sp_DangKi"))
            {
                try
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Connection = DataProvider.Con;
                    int role = 0;
                    cmd.Parameters.Add(new SqlParameter("@membreOuEmploy", role));
                    cmd.Parameters.Add(new SqlParameter("@type", "Normal"));
                    cmd.Parameters.Add(new SqlParameter("@userName", txtUsername.Text));
                    if (!txtPassword.Password.Equals(txtRePassword.Password))
                    {
                        throw new Exception("Password ne correspond pas à CofirmPassword");
                    }
                    cmd.Parameters.Add(new SqlParameter("@password", (new MD5Hash()).Hash(txtPassword.Password)));
                    cmd.Parameters.Add(new SqlParameter("@nom", txtNom.Text));
                    cmd.Parameters.Add(new SqlParameter("@naissance", Convert.ToDateTime(dtNaissance.Text)));
                    cmd.Parameters.Add(new SqlParameter("@telephone", txtTelephone.Text));
                    if (!(new EmailChecking()).checkFormat(txtCourier.Text))
                    {
                        throw new Exception("Invalide d'adresse de courrier");
                    }
                    cmd.Parameters.Add(new SqlParameter("@email", txtCourier.Text));
                    cmd.Parameters.Add(new SqlParameter("@numIden", txtNoIden.Text));

                    try
                    {
                        cmd.ExecuteNonQuery();

                        MessageBox.Show("Votre enregistrement est complété.", "Resultat d'Enregistrement");
                        childEnregis.Close();
                    }
                    catch (SqlException sqlex)
                    {
                        MessageBox.Show(sqlex.Message);
                    }
                    finally
                    {
                        cmd.Dispose();
                    }
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void btnDaccord_Click(object sender, RoutedEventArgs e)
        {
            using (SqlCommand command = new SqlCommand("dbo.sp_DangNhap"))
            {
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = DataProvider.Con;
                command.Parameters.Add(new SqlParameter("@username", txtUN.Text));
                command.Parameters.Add(new SqlParameter("@pass", (new MD5Hash()).Hash(txtPW.Password)));

                try
                {
                    Object o = command.ExecuteScalar();
                    if ((int)o == 0)
                    {
                        MessageBox.Show("Incorrection de Username ou Password");
                    }
                    else
                    {
                        GetInfoPerson();
                        txtNomUser.Text = "Bonjour " + personne.Nom;
                        btnEntrer.Content = "Deconexion";
                        btnGestion.Visibility = Visibility.Visible;
                        childEntrer.Close();
                    }
                }
                catch (SqlException ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    command.Dispose();
                }
            }

        }

        private void btnHome_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            pageTransitionControl.ShowPage(pages[0]);
        }

        private void btnVoir_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            pageTransitionControl.ShowPage(pages[1]);
        }

        private void btnFilm_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            pageTransitionControl.ShowPage(pages[2]);
        }

        private void btnIntro_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            pageTransitionControl.ShowPage(pages[3]);
        }

        private void btnHome_MouseEnter(object sender, MouseEventArgs e)
        {
            btnHome.Source = new BitmapImage(new Uri("/Images/home2.png", UriKind.Relative));
        }

        private void btnHome_MouseLeave(object sender, MouseEventArgs e)
        {
            btnHome.Source = new BitmapImage(new Uri("/Images/home1.png", UriKind.Relative));
        }

        private void btnVoir_MouseEnter(object sender, MouseEventArgs e)
        {
            btnVoir.Source = new BitmapImage(new Uri("/Images/cal2.png", UriKind.Relative));
        }

        private void btnVoir_MouseLeave(object sender, MouseEventArgs e)
        {
            btnVoir.Source = new BitmapImage(new Uri("/Images/cal1.png", UriKind.Relative));
        }

        private void btnFilm_MouseEnter(object sender, MouseEventArgs e)
        {
            btnFilm.Source = new BitmapImage(new Uri("/Images/film2.png", UriKind.Relative));
        }

        private void btnFilm_MouseLeave(object sender, MouseEventArgs e)
        {
            btnFilm.Source = new BitmapImage(new Uri("/Images/film1.png", UriKind.Relative));
        }

        private void btnIntro_MouseEnter(object sender, MouseEventArgs e)
        {
            btnIntro.Source = new BitmapImage(new Uri("/Images/info2.png", UriKind.Relative));
        }

        private void btnIntro_MouseLeave(object sender, MouseEventArgs e)
        {
            btnIntro.Source = new BitmapImage(new Uri("/Images/info1.png", UriKind.Relative));
        }

        private void btnAnnuler_Click(object sender, RoutedEventArgs e)
        {
            childEnregis.Close();
        }

        private void txtNoIden_TextChanged(object sender, TextChangedEventArgs e)
        {
            txtNoIden.Text = (new NumerableText()).filterNumber(txtNoIden.Text);
        }

        private void txtTelephone_TextChanged(object sender, TextChangedEventArgs e)
        {
            txtTelephone.Text = (new NumerableText()).filterNumber(txtTelephone.Text);
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            childEntrer.Close();
        }

        private void GetInfoPerson()
        {
            personne = new Personne();
            using(SqlCommand command = new SqlCommand("dbo.spGetInfoPerson"))
            {
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Add(new SqlParameter("@username", txtUN.Text));
                command.Connection = DataProvider.Con;

                try
                {
                    using (SqlDataReader reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            personne.Id = reader.GetString(reader.GetOrdinal("IDPerson"));
                            personne.Username = reader.GetString(reader.GetOrdinal("Username"));
                            personne.Password = reader.GetString(reader.GetOrdinal("Password"));
                            personne.Nom = reader.GetString(reader.GetOrdinal("Nom"));
                            personne.Naissance = reader.GetDateTime(reader.GetOrdinal("Naissance"));
                            personne.Telephone = reader.GetString(reader.GetOrdinal("Telephone"));
                            personne.Email = reader.GetString(reader.GetOrdinal("Email"));
                            personne.NumIden = reader.GetString(reader.GetOrdinal("NumIdentiter"));
                        }
                    }
                }
                catch (SqlException ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    command.Dispose();
                }
            }
        }

        private void btnGestion_Click(object sender, RoutedEventArgs e)
        {
            //load info
            tbGID.Text = "ID - " + personne.Id;
            tbGUsername.Text = "Username - " + personne.Username;
            txtGPassword.Password = personne.Password;
            txtGNom.Text = personne.Nom;
            txtGTelephone.Text = personne.Telephone;
            txtGEmail.Text = personne.Email;
            tbGNumIden.Text = "Numero d'Identifier - " + personne.NumIden;
            dtGNaissance.Value = personne.Naissance;
            //show
            childGestion.Show();
        }

        private void imgGPassword_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            spChangePass.Visibility = Visibility.Visible;
        }

        private void imgGNom_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            txtGNom.IsEnabled = true;
        }

        private void imgGTele_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            txtGTelephone.IsEnabled = true;
        }

        private void imgGEmail_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            txtGEmail.IsEnabled = true;
        }

        private void btnCOK_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (!(new MD5Hash()).Hash(txtCOldPass.Password).Equals(txtGPassword.Password))
                {
                    throw new Exception("Nouveau mot de passe ne correspond pas");
                }
                if (txtCNewPass.Password == null || txtCNewPass.Password.Trim() == ""
                    || txtCConfirmPass.Password == null || txtCConfirmPass.Password.Trim() == "")
                {
                    throw new Exception("Valeur nulle n'est pas autorisé");
                }

                personne.Password = (new MD5Hash()).Hash(txtCNewPass.Password);
                spChangePass.Visibility = Visibility.Collapsed;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnCCancel_Click(object sender, RoutedEventArgs e)
        {
            spChangePass.Visibility = Visibility.Collapsed;
        }

        private void imgGPassword_MouseEnter(object sender, MouseEventArgs e)
        {
            imgGPassword.Source = new BitmapImage(new Uri("/Images/edit2.png", UriKind.Relative));
        }

        private void imgGPassword_MouseLeave(object sender, MouseEventArgs e)
        {
            imgGPassword.Source = new BitmapImage(new Uri("/Images/edit1.png", UriKind.Relative));
        }

        private void imgGNom_MouseEnter(object sender, MouseEventArgs e)
        {
            imgGNom.Source = new BitmapImage(new Uri("/Images/edit2.png", UriKind.Relative));
        }

        private void imgGNom_MouseLeave(object sender, MouseEventArgs e)
        {
            imgGNom.Source = new BitmapImage(new Uri("/Images/edit1.png", UriKind.Relative));
        }

        private void imgGTele_MouseEnter(object sender, MouseEventArgs e)
        {
            imgGTele.Source = new BitmapImage(new Uri("/Images/edit2.png", UriKind.Relative));
        }

        private void imgGTele_MouseLeave(object sender, MouseEventArgs e)
        {
            imgGTele.Source = new BitmapImage(new Uri("/Images/edit1.png", UriKind.Relative));
        }

        private void imgGEmail_MouseEnter(object sender, MouseEventArgs e)
        {
            imgGEmail.Source = new BitmapImage(new Uri("/Images/edit2.png", UriKind.Relative));
        }

        private void imgGEmail_MouseLeave(object sender, MouseEventArgs e)
        {
            imgGEmail.Source = new BitmapImage(new Uri("/Images/edit1.png", UriKind.Relative));
        }

        private void txtGNom_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                try
                {
                    if (txtGNom.Text.Trim() == "")
                    {
                        throw new Exception("Valeur nulle n'est pas autorisé");
                    }
                    personne.Nom = txtGNom.Text;
                    txtGNom.IsEnabled = false;
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void txtGTelephone_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                personne.Telephone = txtGTelephone.Text;
                txtGTelephone.IsEnabled = false;
            }
        }

        private void txtGEmail_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                try
                {
                    if (!(new EmailChecking()).checkFormat(txtGEmail.Text))
                    {
                        throw new Exception("Invalide d'adresse de courrier");
                    }
                    personne.Email = txtGEmail.Text;
                    txtGEmail.IsEnabled = false;
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void txtGTelephone_TextChanged(object sender, TextChangedEventArgs e)
        {
            txtGTelephone.Text = (new NumerableText()).filterNumber(txtGTelephone.Text);
        }

        private void imgEnreg_MouseEnter(object sender, MouseEventArgs e)
        {
            imgEnreg.Source = new BitmapImage(new Uri("/Images/ok2.png", UriKind.Relative));
        }

        private void btnCOK_MouseEnter(object sender, MouseEventArgs e)
        {
            btnCOK.Source = new BitmapImage(new Uri("/Images/ok2.png", UriKind.Relative));
        }

        private void btnCOK_MouseLeave(object sender, MouseEventArgs e)
        {
            btnCOK.Source = new BitmapImage(new Uri("/Images/ok1.png", UriKind.Relative));
        }

        private void btnCCancel_MouseEnter(object sender, MouseEventArgs e)
        {
            btnCCancel.Source = new BitmapImage(new Uri("/Images/cancel2.png", UriKind.Relative));
        }

        private void btnCCancel_MouseLeave(object sender, MouseEventArgs e)
        {
            btnCCancel.Source = new BitmapImage(new Uri("/Images/cancel1.png", UriKind.Relative));
        }

        private void imgEnreg_MouseLeave(object sender, MouseEventArgs e)
        {
            imgEnreg.Source = new BitmapImage(new Uri("/Images/ok1.png", UriKind.Relative));
        }

        private void imgAnnu_MouseEnter(object sender, MouseEventArgs e)
        {
            imgAnnu.Source = new BitmapImage(new Uri("/Images/cancel2.png", UriKind.Relative));
        }

        private void imgAnnu_MouseLeave(object sender, MouseEventArgs e)
        {
            imgAnnu.Source = new BitmapImage(new Uri("/Images/cancel1.png", UriKind.Relative));
        }

        private void imgEnreg_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            try
            {
                using (SqlCommand command = new SqlCommand("dbo.spMisAJourPersonne"))
                {
                    command.CommandType = CommandType.StoredProcedure;
                    command.Connection = DataProvider.Con;
                    command.Parameters.Add(new SqlParameter("@IDPerson", personne.Id));
                    command.Parameters.Add(new SqlParameter("@password", personne.Password));
                    command.Parameters.Add(new SqlParameter("@nom", personne.Nom));
                    command.Parameters.Add(new SqlParameter("@naissance", personne.Naissance));
                    command.Parameters.Add(new SqlParameter("@telephone", personne.Telephone));
                    command.Parameters.Add(new SqlParameter("@email", personne.Email));

                    command.ExecuteNonQuery();
                    childGestion.Close();
                }
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void imgAnnu_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            childGestion.Close();
        }

        private void btnDaccord_MouseLeave(object sender, MouseEventArgs e)
        {
            btnDaccord.Source = new BitmapImage(new Uri("/Images/ok1.png", UriKind.Relative));
        }

        private void btnDaccord_MouseEnter(object sender, MouseEventArgs e)
        {
            btnDaccord.Source = new BitmapImage(new Uri("/Images/ok2.png", UriKind.Relative));
        }

        private void btnCancel_MouseEnter(object sender, MouseEventArgs e)
        {
            btnCancel.Source = new BitmapImage(new Uri("/Images/cancel2.png", UriKind.Relative));
        }

        private void btnCancel_MouseLeave(object sender, MouseEventArgs e)
        {
            btnCancel.Source = new BitmapImage(new Uri("/Images/cancel1.png", UriKind.Relative));
        }

        private void btnEnregistrer_MouseLeave(object sender, MouseEventArgs e)
        {
            btnEnregistrer.Source = new BitmapImage(new Uri("/Images/ok1.png", UriKind.Relative));
        }

        private void btnEnregistrer_MouseEnter(object sender, MouseEventArgs e)
        {
            btnEnregistrer.Source = new BitmapImage(new Uri("/Images/ok2.png", UriKind.Relative));
        }

        private void btnAnnuler_MouseEnter(object sender, MouseEventArgs e)
        {
            btnAnnuler.Source = new BitmapImage(new Uri("/Images/cancel2.png", UriKind.Relative));
        }

        private void btnAnnuler_MouseLeave(object sender, MouseEventArgs e)
        {
            btnAnnuler.Source = new BitmapImage(new Uri("/Images/cancel1.png", UriKind.Relative));
        }
    }
}